//
//  ViewController.swift
//  Pomodoro
//
//  Created by zhengju on 2018/3/1.
//  Copyright © 2018年 zhengju. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    var taskField: UITextField?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.title = "番茄钟"
        
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "任务", style: .plain, target: self, action: #selector(leftBarAction))
        
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "设置", style: .plain, target: self, action: #selector(rightBarAction))

        let countDownView = CountDownView.init(frame: CGRect.init(x: 0, y: 64, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - 64))
        self.view.addSubview(countDownView)
        countDownView.timerComplete = {[weak self] in
            print("完成了一个番茄法")
            self?.addTask()
        }
    }
    @objc func leftBarAction() {
        self.navigationController?.pushViewController(TaskListController(), animated: true)
    }
    @objc func rightBarAction() {
        self.navigationController?.pushViewController(SetupController(), animated: true)
    }
    func addTask()  {
        
        let alertView = UIAlertController(title: "提示", message: "添加任务", preferredStyle: .alert)
        
        alertView.addTextField { (textField) in
            self.taskField = textField;
        }
        
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        
        let okAction = UIAlertAction(title: "确定", style: .default, handler:{
            (UIAlertAction) -> Void in

            print(self.taskField!.text! as Any)
            
            let vc = TaskListController()
        
            vc.addTitle = self.taskField!.text!
            
            self.navigationController?.pushViewController(vc, animated: true)
        })
        
        alertView.addAction(cancelAction)
        
        alertView.addAction(okAction)// 当添加的UIAlertAction超过两个的时候，会自动变成纵向分布
        
        self.present(alertView, animated: true, completion: nil)
    }
}

